package com.rpp;

import com.rpp.core.Connector;
import com.rpp.core.Mapper;
import com.rpp.core.StandardServer;
import com.rpp.core.StandardService;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Description
 * @Author rpp
 * @Date 2020/7/18 2:51 下午
 */
public class Bootstrap {

    private StandardServer server;

    /**
     * Minicat 的程序启动入口
     *
     * @param args
     */
    public static void main(String[] args) {
        Bootstrap bootstrap = new Bootstrap();
        try {
            // 启动Minicat
            bootstrap.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 启动方法
     *
     * @throws IOException
     */
    private void start() throws Exception {

        //初始化server，主要就是解析server.xml封装Connector,Engine ,Host,Context,Servlet
        initServer();

        //初始化默认Wapper，如果没有自定义context，需要匹配默认context, 就是当前项目中的资源
        server.getServices().getMapper().initDefaultWapper();

        System.out.println("server 初始化完成。。。");

        //启动服务，根据上面解析出的信息监听不同端口，并处理资源
        server.start();
    }

    /**
     * 初始化server
     *
     * @return
     */
    public void initServer() {
        if (server != null) {
            return;
        }
        //初始化server和service
        server = new StandardServer();
        StandardService service = new StandardService();
        server.setServices(service);

        //初始化mapper并与service绑定
        Mapper mapper = new Mapper(service);
        service.setMapper(mapper);

        //解析server.xml封装Connector  Engine  Host Context
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("server.xml");
        SAXReader saxReader = new SAXReader();

        Document document = null;
        try {
            document = saxReader.read(resourceAsStream);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        //读取Service标签
        Element rootElement = document.getRootElement();
        List<Element> selectNodes = rootElement.selectNodes("//Service");

        //读取Connector标签
        for (Element element : selectNodes) {
            List<Element> connectorList = element.selectNodes("//Connector");
            for (Element connectorElement : connectorList) {
                //解析Connector标签下port元素的值，封装Connector,并放入Service
                String port = connectorElement.attributeValue("port");
                Connector connector = new Connector();
                connector.setPort(Integer.parseInt(port));
                service.addConnector(connector);
                connector.setService(service);
            }

            //解析Service标签下的Engine，一个Service下对应一个Engine
            Element engineElement = (Element) element.selectSingleNode("Engine");
            //解析Engine标签下的Host，一个Engine下对应多个Host
            List<Element> hostElements = engineElement.selectNodes("//Host");

            for (Element hostElement : hostElements) {
                //封装MappedHost
                String name = hostElement.attributeValue("name");
                String appBase = hostElement.attributeValue("appBase");
                mapper.addHost(name, appBase);

                //解析Context标签封装 MappedContext
                List<Element> contextList = engineElement.selectNodes("//Context");
                for (Element conextElement : contextList) {
                    String docBase = conextElement.attributeValue("docBase");
                    String path = conextElement.attributeValue("path");
                    //往对应host添加context
                    mapper.addContext(name, appBase, docBase, path);
                }
            }
        }
        //初始化Wapper
        service.getMapper().initWapper();

    }
}
